const util = require('../../../utils/util.js')
const app = getApp();
Page({
  data: {
    value: '',
    stype: '',
    placeholder: '',
    movies: {},
    cinemas: {},
    history: []
  },
  onLoad(query) {
   this.initPage(query)
    this.setData({
      history: wx.getStorageSync("history") || []
    })
  },
  initPage(query){
    //搜索类型，-1代表搜索影院或电影，2代表搜索影院
    const stype = query.stype
    let placeholder = ''
    if (stype === '-1') {
      placeholder = '搜电影'
      this.getHotSearchMovies()
    } else {
      placeholder = '搜影院'
    }
    this.setData({
      stype,
      placeholder
    })
  },
  search(e) {
    wx.showLoading({
      title: '加载中...',
    })
    var value = e.detail.value
    const _this = this
    console.log(e.target.dataset.index)
    if(e.target.dataset.index!=null){
      value = _this.data.history[e.target.dataset.index]
    }else{
      let history = wx.getStorageSync("history") || [];
      history.push(value)
      wx.setStorageSync("history", history);
    }
    wx.request({
      url: app.globalData.url +`/movie/getMovies?cityId=${app.globalData.selectCity.cityId}&search=${value}`,
      method: 'GET',
      header: {
        'content-type': 'application/x-www-form-urlencoded'
      },
      success(res) {
        _this.setData({
          value,
          movies: res.data.data ? res.data.data : [],
          history: []
          // cinemas: res.data.data.cinemaVos ? res.data.data.cinemaVos : []
        })
        wx.hideLoading();
      }
    })
  },
  getHotSearchMovies(){  // 获取十个热门搜索影片
    const _this = this
    wx.request({
      url: app.globalData.url +`/movie/getHotSearchMovies`,
      method: 'GET',
      header: {
        'content-type': 'application/x-www-form-urlencoded'
      },
      success(res) {
        console.log(res.data.data);
        _this.setData({
          history: res.data.data ? res.data.data : [],
        })
      }
    })
  },
  toDeatil(e){
    const {movieId, name} = e.currentTarget.dataset.item;
    console.log(e.currentTarget.dataset.item);
    wx.navigateTo({
      url: `/pages/subPages/select-cinema/select-cinema?movieId=${movieId}&areaName=${name}`,
    })
  },
  goBack() {
    wx.navigateBack({
      delta: 1
    })
  },
  //清除缓存历史
  clearHistory: function () {
    this.setData({
      history: []
    })
    wx.setStorageSync("history", [])
  },
})